package com.zhiyou100.controller.admin;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageInfo;
import com.zhiyou100.model.Course;
import com.zhiyou100.model.Subject;
import com.zhiyou100.service.CourseService;
import com.zhiyou100.service.SubjectService;

@Controller
@RequestMapping("/admin/course")
public class AdminCourseController {
	@Autowired
	private CourseService cs;
	@Autowired
	private SubjectService ss;
	@RequestMapping("/manage")
	public String courseManage(Model mo,Integer pageNum,HttpSession session) {
		int pageSize = 6;
		Integer p = (Integer) session.getAttribute("pages");
		if(pageNum==null) {
			pageNum=1;

		}
		if(pageNum<1) {
			pageNum=1;
		}
		if(p!=null) {
			if(pageNum>p) {
				pageNum = p;
			}
		}
		PageInfo<Course> pageInfo = cs.selectCourseAll(pageNum,pageSize);
		List<Course> courses = pageInfo.getList();
		int pages = pageInfo.getPages();
		int pNum = pageInfo.getPageNum();
		if(pageNum>pages) {
			pageNum = pages;
		}
		session.setAttribute("pages", pages);
		mo.addAttribute("courses", courses);
		session.setAttribute("pageNum", pNum);
		List<Subject> subjectNames = ss.selectSubjectName();
		session.setAttribute("subjects", subjectNames);
		return "forward:/back/course/course_list.jsp";
	}

	@RequestMapping("/editCourse")
	public String editCourse(Integer id,Model mo,HttpSession session) {
		Course course = cs.selectCourse(id);
		List<Subject> subjectNames = ss.selectSubjectName();
		mo.addAttribute("course", course);
		session.setAttribute("subjects", subjectNames);
		Date date = new Date();
		Long time = date.getTime();
		session.setAttribute("time", time);
		return "forward:/back/course/edit_course.jsp";
	}
	@RequestMapping("/updateCourse")
	public String updateCourse(Course course,HttpSession session,String randomCode) {
		Integer  pageNum = (Integer) session.getAttribute("pageNum");
		Long time = (Long) session.getAttribute("time");
		if(time==null) {
			return "redirect:/admin/course/manage?pageNum="+pageNum;
		}
		if(time==Long.parseLong(randomCode)) {
			int i = cs.updateCourse(course);
			session.setAttribute("time", null);	
		}
		return "redirect:/admin/course/manage?pageNum="+pageNum;
	}
	@RequestMapping("/forAddCourse")
	public String forAddCourse(HttpSession session) {
		List<Subject> subjectNames = ss.selectSubjectName();
		session.setAttribute("subjects", subjectNames);
		Date date = new Date();
		long time = date.getTime();
		session.setAttribute("time", time);
		return "forward:/back/course/edit_course.jsp";
	}

	@RequestMapping("/addCourse")
	public String addCourse(Course course,HttpSession session,String randomCode) {
		Integer  pageNum = (Integer) session.getAttribute("pageNum");
		Long time = (Long) session.getAttribute("time");
		if(time==null) {
			return "redirect:/admin/course/manage?pageNum="+pageNum;
		}
		if(time==Long.parseLong(randomCode)) {
			int i = cs.addCourse(course);
			session.setAttribute("time", null);	
		}
		return "redirect:/admin/course/manage?pageNum="+pageNum;
	}
	@RequestMapping("/deleteCourse")
	public String deleteCourse(Integer id,HttpSession session) {
		Integer pageSize = 6;
		int i = cs.deleteCourse(id);
		Integer  pageNum = (Integer) session.getAttribute("pageNum");
		return "redirect:/admin/course/manage?pageNum="+pageNum;
	}
}
